package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.domain.entity.Article;
import com.example.domain.ResponseResult;
import com.example.domain.entity.Category;
import com.example.domain.vo.ArticleDetailVo;
import com.example.domain.vo.ArticleListVo;
import com.example.domain.vo.HotArticleVo;
import com.example.domain.vo.PageVo;
import com.example.mapper.ArticleMapper;
import com.example.service.ArticleService;
import com.example.service.CategoryService;
import com.example.utils.BeanCopyUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;


@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService {

    @Autowired
    private CategoryService categoryService;

    /**
     * 热门文章
     * @return
     */
    @Override
    public ResponseResult hotArticle() {
        //构造查询条件
        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Article::getStatus,0);
        queryWrapper.orderByDesc(Article::getViewCount);
        //分页构造器
        Page<Article> page = new Page(1,10); // -- 当前页数 -- 每页总数
        page(page,queryWrapper);
        //vo返回
        List<Article> records = page.getRecords();
        List<HotArticleVo> lists = new ArrayList<>();
        for (Article article : records) {
            HotArticleVo hotArticleVo = new HotArticleVo();
            BeanUtils.copyProperties(article,hotArticleVo);
            lists.add(hotArticleVo);
        }
        return ResponseResult.okResult(lists);

    }

    @Override
    public ResponseResult articleList(Integer pageNum,Integer pageSize,Long categoryId) {
        //分页查询 构造条件  --对置顶的文章--状态正常的文章  categoryId不一定传
        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(Objects.nonNull(categoryId) && categoryId > 0,Article::getCategoryId,categoryId);
        queryWrapper.eq(Article::getStatus,0);
        queryWrapper.orderByAsc(Article::getIsTop);
        //分页
        Page<Article> page = new Page<>(pageNum,pageSize);
        page(page,queryWrapper);
        //封装查询结果 ArticleListVo
        List<Article> records = page.getRecords();

        List<ArticleListVo> results = BeanCopyUtils.copyBeanList(records, ArticleListVo.class);
        //封装分类名称
        for (ArticleListVo vo :results) {
            Long categoryIdOne = vo.getCategoryId();
            String categoryName = categoryService.getById(categoryIdOne).getName();
            vo.setCategoryName(categoryName);

        }
        //PageVo
        PageVo pageVo = new PageVo(results,page.getTotal());
        return ResponseResult.okResult(pageVo);

    }

    @Override
    public ResponseResult getArticleDetails(Long id) {
        Article article = getById(id);
        //获取分类名称
        ArticleDetailVo articleDetailVo = BeanCopyUtils.copyBean(article, ArticleDetailVo.class);
        Category category = categoryService.getById(articleDetailVo.getCategoryId());
        if (category.getName() != null){
            articleDetailVo.setCategoryName(category.getName());
        }
        return ResponseResult.okResult(articleDetailVo);
    }
}
